package com.yanqu.road.server.gameplayer.module.activity.horserun.event;

import com.yanqu.road.entity.activity.horserun.HorseRunUserGamingData;
import com.yanqu.road.entity.activity.horserun.config.HorseRunEventConfig;
import com.yanqu.road.entity.enums.activity.horserun.eHorseRunAttrEnum;
import com.yanqu.road.logic.activity.horserun.HorseRunConfig;
import com.yanqu.road.pb.activity.HorseRunProto;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 探索事件父事件 处理转入子事件  exploreStart("探索开始事件 可以选择结束和继续",6),
 */

public class HorseRunExploreParentEvent extends HorseRunBaseEvent{
    @Override
    public HorseRunProto.EventResult.Builder meet(HorseRunUserGamingData userGamingData, HorseRunConfig config, HorseRunEventConfig eventConfigById) {

        HorseRunProto.EventResult.Builder builder = HorseRunProto.EventResult.newBuilder();
        int eventId = eventConfigById.getId();
        builder.setError(0);

        userGamingData.setRemainExploreTime(config.getExploreEventMaxTime());

        addUserLog(userGamingData,config,1,eventId,new ArrayList<>(),builder);
        eventGoing(userGamingData);

        return builder;
    }

    @Override
    public HorseRunProto.EventResult.Builder deal(HorseRunUserGamingData userGamingData, HorseRunDealEventArg arg, HorseRunConfig config, HorseRunEventConfig eventConfigById) {
        HorseRunProto.EventResult.Builder builder = HorseRunProto.EventResult.newBuilder();
        if (arg.getExploreOneArg() != 0 && arg.getExploreOneArg() != 1){
            builder.setError(GameErrorCode.E_HORSE_RUN_ARG_ERROR);
            return builder;
        }
        if (arg.getExploreOneArg() == 1 && userGamingData.getRemainExploreTime() <= 0){
            builder.setError(GameErrorCode.E_HORSE_RUN_REMAIN_TIMES_NO_ENOUGH);
            return builder;
        }
        int randomExploreEventId = config.getRandomExploreEventId();
        if (randomExploreEventId <= 0){
            builder.setError(GameErrorCode.E_HORSE_RUN_CONFIG_ERROR);
            return builder;
        }
        HorseRunEventConfig exploreConfig = config.getEventConfigById(randomExploreEventId);
        if (exploreConfig == null){
            builder.setError(GameErrorCode.E_HORSE_RUN_CONFIG_ERROR);
            return builder;
        }

        if (arg.getExploreOneArg() == 1){
            userGamingData.setRemainExploreTime(userGamingData.getRemainExploreTime() - 1);

            //是否增益，0为负面 1为增益
            List<Integer> paramList = exploreConfig.getParamList();
            int addOrReduce = paramList.get(4);

            int exp = paramList.get(3);
            int energy = paramList.get(0);
            int artful = paramList.get(1);
            int resistance = paramList.get(2);

            if (addOrReduce == 0){
                exp = -exp;
                energy = -energy;
                artful = -artful;
                resistance = -resistance;
            }
            List<Integer> logParam = new ArrayList<>();
            //第一个参数一定是剩余次数
            logParam.add(userGamingData.getRemainExploreTime());

            if (Math.abs(exp) > 0) {
                exp = exp * userGamingData.getOpenGameMultiple();
                userGamingData.addExpSum(exp);
                builder.setExpAdd(exp);


                logParam.add(0);
                logParam.add(exp);

                addUserLog(userGamingData,config,4,randomExploreEventId,logParam,builder);
            }
            if (Math.abs(energy) > 0) {
                userGamingData.addBaseAttr(eHorseRunAttrEnum.energy.getValue(),energy);
                HorseRunProto.AttrRiseResult.Builder attrAdd = HorseRunProto.AttrRiseResult.newBuilder();
                attrAdd.setType(eHorseRunAttrEnum.energy.getValue());
                attrAdd.setValue(energy);

                builder.addAttrAdd(attrAdd);


                logParam.add(eHorseRunAttrEnum.energy.getValue());
                logParam.add(energy);
                addUserLog(userGamingData,config,4,randomExploreEventId,logParam,builder);
            }
            if (Math.abs(artful) > 0) {
                userGamingData.addBaseAttr(eHorseRunAttrEnum.artful.getValue(),artful);
                HorseRunProto.AttrRiseResult.Builder attrAdd = HorseRunProto.AttrRiseResult.newBuilder();
                attrAdd.setType(eHorseRunAttrEnum.artful.getValue());
                attrAdd.setValue(artful);
                builder.addAttrAdd(attrAdd);


                logParam.add(eHorseRunAttrEnum.artful.getValue());
                logParam.add(artful);
                addUserLog(userGamingData,config,4,randomExploreEventId,logParam,builder);
            }
            if (Math.abs(resistance) > 0) {
                userGamingData.addBaseAttr(eHorseRunAttrEnum.resistance.getValue(),resistance);
                HorseRunProto.AttrRiseResult.Builder attrAdd = HorseRunProto.AttrRiseResult.newBuilder();
                attrAdd.setType(eHorseRunAttrEnum.resistance.getValue());
                attrAdd.setValue(resistance);
                builder.addAttrAdd(attrAdd);

                logParam.add(eHorseRunAttrEnum.resistance.getValue());
                logParam.add(resistance);
                addUserLog(userGamingData,config,4,randomExploreEventId,logParam,builder);
            }

            builder.setEventMove(randomExploreEventId);
            if (userGamingData.getRemainExploreTime() <= 0){
                //没次数就结束掉
                eventDone(userGamingData);
            }
        }else {
            userGamingData.setRemainExploreTime(0);
            addUserLog(userGamingData,config,2, userGamingData.getEventId(), new ArrayList<>(),builder);
            eventDone(userGamingData);
        }

        return builder;
    }
}
